<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\DB;
use think\Controller;

class Order extends Base
{
    
    
    // 订单列表
    public function orderList()
    {   
        $where = [];
        $where['admin_id'] = $this->accountId;
        if ($this->accountId == 1300) {
            $where['genre'] = 'goods';
        }
        $field = ['request_mess', 'return_mess', 'param'];
        $res = db('order')->where($where)->field($field, true)->order('id desc')->paginate($this->page, false);
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();

       
    }
 /**
     *商品详情列表展示
     * 
     */
    public function goods_detailList()
    {
        //return input("order_id");
        $where['order_id'] = input("order_id"); //接收订单ID，判断条件一定要记住
        //$where['admin_id'] = $this->accountId;
        $res = db('order_goods')->where($where)->order('create_time desc')->select();
        //echo DB::name('order_goods')->getlastsql();
        // foreach ($res as $k=>$v){
        //     $param[$k] = json_decode($v['param'],true);
        //     //dump($param[$k]);exit;
        //     $p = '';
        //         foreach ($param[$k] as $key => $value) {
        //             $pv =  implode(',',$param[$k][$key]['val']); 
        //             @$p .= '{'.$param[$k][$key]['key'].':'.$pv.'}';
        //         }
        //         $r[$k] = $v;
        //         $r[$k]['param'] = $p;

            
        // }
        //dump($res);exit;
        //$page = $res->render();
        $this->assign('res', $res);
        //$this->assign('page', $page);
        return $this->fetch();
    }



    //订单列表-修改
    public function orderedit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $where = [];
            $where['id'] = $id;
            $where['admin_id'] = $this->accountId;
            $res = db('order')->where($where)->update($post);
            if ($res) {
                suc('','修改成功');
            }else{
                suc('','修改失败');
            }
        }

        $res = db('order')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();

    }
    // 用户历史记录
    public function userHistory()
    {
        $where = [];
        $where['admin_id'] = $this->accountId;
        $res = db('history')->where($where)->paginate($this->page, false);
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    // 广告 - 列表 
    public function adList()
    {        
        $res = db('ad')
            ->where(['admin_id'=>$this->accountId, 'type'=>'bbs'])
            ->field('id, display, title, path, thumb, sort')
            ->order('sort desc')
            ->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    // 广告 - 修改
    public function adEdit($id)
    {
        if (request()->isPost()) {
            $post = request()->only(['title', 'path', 'sort'], 'post');
            $where = [];
            $where['id'] = $id;
            $where['admin_id'] = $this->accountId;
            $res = db('ad')->where($where)->update($post);
            suc();
        }
        
        $res = db('ad')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    // 广告 - 增加
    public function adAdd()
    {
        if (request()->isPost()) {
            $post = request()->only(['title', 'path', 'sort'], 'post');
            $post['admin_id'] = $this->accountId;
            return $this->BbsLogic->adAdd($post);
        }
        return $this->fetch();
    }
    
    // 广告 - 删除
    public function adDel($id)
    {
        $where = [];
        $where['id'] = $id;
        $where['admin_id'] = $this->accountId;
        $res = db('ad')->where($where)->delete();
        suc();
    }
    
    public function adAdds()
    {
        return json_encode($_REQUEST);
    }
    
}
